package com.zhenmaitang.clinic_sys.controller;

import com.zhenmaitang.clinic_sys.common.AuthResultCode;
import com.zhenmaitang.clinic_sys.common.ResultCode;
import com.zhenmaitang.clinic_sys.dto.LoginRequest;
import com.zhenmaitang.clinic_sys.dto.UserRegistrationRequest;
import com.zhenmaitang.clinic_sys.exception.BusinessException;
import com.zhenmaitang.clinic_sys.service.AuthService;
import com.zhenmaitang.clinic_sys.util.ApiResponse;
import com.zhenmaitang.clinic_sys.vo.UserVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/auth")
@CrossOrigin(origins = "*")
@Tag(name = "认证管理", description = "用户认证相关接口")
public class AuthController {

    @Autowired
    private AuthService authService;

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户使用用户名和密码登录系统")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "登录成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "用户名或密码错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "账号已被禁用"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public ApiResponse<UserVO> login(
            @Parameter(description = "登录请求参数", required = true) @Valid @RequestBody LoginRequest request) {
        try {
            UserVO authData = authService.login(request);
            return ApiResponse.success(AuthResultCode.LOGIN_SUCCESS,authData);

        } catch (BusinessException e) {
            return ApiResponse.error(e);
        } catch (Exception e) {
            return ApiResponse.error(ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "注册成功并返回登录信息")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "注册成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "注册失败"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "409", description = "用户名或邮箱已存在")
    })
    public ApiResponse<UserVO> register(
            @Parameter(description = "注册请求参数", required = true) @Valid @RequestBody UserRegistrationRequest request) {
        try {
            UserVO authData = authService.register(request);
            return ApiResponse.success("注册并登录成功",authData);
        } catch (BusinessException e) {
            return ApiResponse.error(e);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(AuthResultCode.SERVICE_UNAVAILABLE);
        }
    }


   
}